package com.yonyou.crm.sys.dataright.handler;

import java.util.ArrayList;
import java.util.List;

import com.yonyou.crm.sys.dataright.entity.RightObject;
import com.yonyou.crm.sys.dataright.entity.SuperVO;

public class OwnerHandler implements RightHandler{

	//权限实体信息
	RightObject rightObj;
	//当前用户
	Long curUser;
	
	public OwnerHandler(RightObject rightObj, Long curUser) {
		super();
		this.rightObj = rightObj;
		this.curUser = curUser;
	}

	@Override
	public boolean hasRight(SuperVO vo) {
		return curUser == getOwner(vo);
	}

	@Override
	public SuperVO[] filterWithRight(SuperVO[] vos) {
		if(vos == null || vos.length == 0) {
			return vos;
		}
		List<Object> result = new ArrayList<Object>();
		for(int i=0,len=vos.length;i<len;i++) {
			if(curUser == getOwner(vos[i])) {
				result.add(vos[i]);
			}
		}
		return result.toArray(new SuperVO[0]);
	}

	@Override
	public String getRightSql() {
		String ownerField = rightObj.getOwnerField();
		return ownerField +"=" + curUser;
	}

	private Long getOwner(SuperVO vo) {
		return vo.getOwner();
	}
}
